 <!--codemirror-text格式化-->
<template>
  <div class="text-editor">
    <textarea ref="textarea"/>
  </div>
</template>


<script>
  import 'codemirror/addon/lint/lint'
  import 'codemirror/addon/lint/lint.css'
  import 'codemirror/lib/codemirror.css'
  import 'codemirror/theme/rubyblue.css'
  import 'codemirror/mode/javascript/javascript'

  import CodeMirror from 'codemirror'
export default {
  name: 'TextEditor',
  /* eslint-disable vue/require-prop-types */
  props: ['value'],
  data () {
    return {
      textEditor: false
    }
  },
  watch: {
    value (value) {
      const editor_value = this.textEditor.getValue()
      if (value !== editor_value) {
        this.textEditor.setValue(this.value)
      }
    }
  },
  mounted () {
    this.textEditor = CodeMirror.fromTextArea(this.$refs.textarea, {
      lineNumbers: true,
      gutters: ['CodeMirror-lint-markers'],
      theme: 'rubyblue',
      lint: true
    })

    this.textEditor.setValue(this.value)
    this.textEditor.on('change', cm => {
      this.$emit('changed', cm.getValue())
      this.$emit('input', cm.getValue())
    })
  },
  methods: {
    getValue () {
      return this.textEditor.getValue()
    }
  }
}
</script>

<style scoped>
.text-editor {
  height: 100%;
  position: relative;
}
.text-editor >>> .CodeMirror {
  height: auto;
  min-height: 180px;
}
.text-editor >>> .CodeMirror-scroll {
  min-height: 180px;
}
.text-editor >>> .cm-s-rubyblue span.cm-string {
  color: #f08047;
}
</style>
